<template>
  <div id="dynamicBg" class="container" :class="isBackgroundIsAll ? 'backgroundIsAll' : 'backgroundIsNoAll'">
    <div id="fixed-top" class="container-top" :class="{ 'fixed-top': isFixed, 'absolute-top': !isFixed }">
      <div class="container-top-box">
        <div class="container-top-box-left">
          <el-menu class="el-menu-demo" mode="horizontal" style="border: none;"
                   background-color="rgba(0,0,0,0)" text-color="#666666" active-text-color="#666666">
            <el-menu-item @click="handleMenuItem(item, index)" :index="item.path" v-for="(item, index) in menulist" :key="item.id">
              <!--                图标-->
              <i :class="item.icon" style="color: rgba(255, 255, 255);"></i>
              <!--                文本-->
              <div style="font-size: 24px;">{{item.title}}</div>
            </el-menu-item>
          </el-menu>
        </div>
        <div class="container-top-box-img" @click="goUrl('/index')" style="cursor: pointer; flex: 1">
          <img :src="logoLight" alt="" >
        </div>
        <div class="container-top-box-right">
          <el-button
            v-if="!name"
            style="background-color: transparent; color: #ffffff"
            size="big"
            @click="goUrl('/cmsLogin')"
          >登录</el-button>
          <el-dropdown v-else class="avatar-container right-menu-item hover-effect" trigger="click">
            <div class="avatar-wrapper">
              <!-- <img :src="avatar" class="user-avatar"> -->
              <el-avatar class="user-avatar" :src="avatar" @error="errorHandler">
                <i class="el-icon-s-custom" />
              </el-avatar>
              <div class="avatar-Name">{{name}}</div>
            </div>
            <el-dropdown-menu slot="dropdown">
              <router-link to="/cms/blog">
                <el-dropdown-item>管理</el-dropdown-item>
              </router-link>
              <el-dropdown-item divided @click.native="logout">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
<!--          <el-button-->
<!--            type="primary"-->
<!--            size="big"-->
<!--          >立即注册</el-button>-->
        </div>
      </div>
    </div>
    <div class="container-center">
      <template v-if="!isBackgroundIsAll">
        <div class="container-center-title">{{ queryTitle }}</div><hr>
        <div class="container-center-desc">{{ queryDesc }}</div>
      </template>
      <template v-else>
<!--        <img :src="backgroundFont" alt="">-->
<!--        <div class="container-center-font">从这里开始，写下你的故事</div>-->
        <el-button
          type="primary"
          size="big"
          style="width: 145px; margin-top: 1.04em"
          @click="goUrl('/read')"
        >开始阅读</el-button>
      </template>
    </div>
  </div>
</template>

<script>
import backgroundFont from "@/assets/images/backgroundFont.png"
import { mapGetters } from 'vuex'
import store from "@/store";
import router from "@/router";

export default {
  name: "topShow",
  data() {
    return {
      backgroundFont,
      logoLight: null,
      findRoute: [],
      menulist: [],
      clickMenuIndex: null,
      picUrl: "",
      logoUrl: "",
      isFixed: false,
      imgUrl: ""
    }
  },
  computed: {
    ...mapGetters([
      'avatar',
      'name'
    ]),
    isBackgroundIsAll() {
      if(this.$route.meta?.isBackgroundIsAll) {
        return true
      } else {
        return false
      }
    },
    queryTitle() {
      return this.$route.query?.title || this.menulist[this.clickMenuIndex]?.titleText
    },
    queryDesc() {
      return this.$route.query?.desc || this.menulist[this.clickMenuIndex]?.description
    }
  },
  created() {
    store.dispatch('GenerateRoutes').then(accessRoutes => {
      this.findRoute = accessRoutes.find(item => item.name === 'Index').children
      this.findRoute = this.findRoute.map(item => {
        return { ...item, ...item.meta}
      })
      this.menulist = this.findRoute.filter(item => item.title !== '首页')
    })
    this.getSetting()
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll);
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll);
  },
  watch: {
    isFixed(newVal) {
      const headElement = document.getElementById('fixed-top');
      if(newVal) {
        headElement.style.backgroundImage = this.imgUrl;
      } else {
        headElement.style.backgroundImage = 'none';
      }
    }
  },
  methods: {
    handleScroll() {
      const top = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
      this.isFixed = top > (window.innerHeight * 0.8);
    },
    getSetting() {
      this.getConfigKey("sys.index.pic").then(response => {
        const element = document.getElementById('dynamicBg');
        this.imgUrl = `url(${process.env.VUE_APP_BASE_API + response.msg})`;
        element.style.backgroundImage = this.imgUrl
      });
      this.getConfigKey("sys.index.logo").then(response => {
        this.logoLight = process.env.VUE_APP_BASE_API + response.msg;
      });
    },
    handleMenuItem(item, index) {
      const pathRegex = /https?:\/\/(?:www\.)?[^\/]+(\/[^\?#]*)(?:\?[^#]*)?(?:#.*)?/;
      const match = item.link.match(pathRegex);
      if (match) {
        window.open(item.link)
      } else {
        this.clickMenuIndex = index
        this.$router.push(item.link)
      }
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/index';
        })
      }).catch(() => {});
    },
    goUrl(url) {
      this.$router.push(url)
    },

  }
}
</script>

<style scoped lang="scss">
.backgroundIsAll {
  height: 100vh;

}
.backgroundIsNoAll {
  height: 50vh;
}
  .container {
    width: 100vw;
    background: url('../../../assets/images/background.jpg') center no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    .fixed-top {
      position: fixed;
      background-color: #FFFFFF;
      top: 0;
      color: #000000;
      z-index: 1000; /* 确保导航栏在顶层 */
    }
    .absolute-top {
      position: absolute;
      top: 1.5em;
      backgroundImage: none;
    }
    .container-top {
      width: 100%;
      transition: top 0.3s;
      img {
        max-height: 1.85em;
      }
      .container-top-box {
        width: 80%;
        display: flex;
        justify-items: center;
        align-items: center;
        justify-content: space-between;
        margin: 0 auto;
        .container-top-box-left {
          display: flex;
          flex: 1;
          justify-content: flex-start;
        }
        .container-top-box-img {
          display: flex;
          flex: 1;
          justify-content: center;
          align-items: center;
        }
        .container-top-box-right {
          display: flex;
          flex: 1;
          justify-content: flex-end;
        }
      }
    }
    .container-center {
      display: flex;
      flex-direction: column;
      align-items: center;
      img {
        width: 40%;
        height: 100%;
      }
      .container-center-font {
        color: #fff;
        font-size: 1.78em;
        line-height: 1.5em;
      }
      .container-center-title {
        color: #fff;
        font-size: 3em;
        line-height: 1.5em;
      }
      .container-center-desc {
        color: #fff;
        font-size: 1em;
        line-height: 1.5em;
      }
    }
  }

  .right-menu-item {
    display: inline-block;
    padding: 0 8px;
    height: 100%;
    font-size: 18px;
    color: #5a5e66;
    vertical-align: text-bottom;

    &.hover-effect {
      cursor: pointer;
      transition: background .3s;

      &:hover {
        background: rgba(0, 0, 0, .025)
      }
    }
  }

  .avatar-container {
    margin-right: 5px;

    .avatar-wrapper {
      margin-top: 5px;
      display: flex;
      align-items: center;
      .user-avatar {
        cursor: pointer;
        border: dashed rgba($color: #ffff7f, $alpha: 0.5);
      }

      .avatar-Name {
        margin-left: 10px;
        cursor: pointer;
        float: right;
        font-size: 16px;
        font-weight: bold;
        color: #5a5e66;
      }
    }
  }
</style>
